package com.realgodo.site.web.user.vocation;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.realgodo.site.entity.vocation.Vocation;
import com.realgodo.site.service.VocationManager;
import com.realgodo.site.web.user.UserDefaultController;

/**
 * @author bijiqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-13上午09:43:43
 * @since jdk1.5
 * @version 0.1
 */
@Result(name = "user_index", type = "redirect", location = "${base}/user/index")
public class EditController extends UserDefaultController {

	private static final long serialVersionUID = -3450717237813857645L;

	@Autowired
	private VocationManager vocationManager;

	/** Vocation.id 组 */
	private Long[] ids;

	private List<Vocation> vocations;
	private List<Vocation> myVocations;

	public String index() throws Exception {
		vocations = this.vocationManager.getAll();
		myVocations = this.vocationManager.getMyVocation(super.getUser());
		for (Vocation outside : vocations) {
			if (outside.getParent() == null) {
				continue;
			}
			for (Vocation inside : myVocations) {
				if (inside.getId().equals(outside.getId())) {
					outside.setChecked(true);
				}
			}
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		this.vocationManager.saveUserVocation(super.getUser(), ids);
		return "user_index";
	}

	public List<Vocation> getVocations() {
		return vocations;
	}

	public void setVocations(List<Vocation> vocations) {
		this.vocations = vocations;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public List<Vocation> getMyVocations() {
		return myVocations;
	}

	public void setMyVocations(List<Vocation> myVocations) {
		this.myVocations = myVocations;
	}

}
